﻿#region Using directives

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="VwDoanhSoProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract class VwDoanhSoProviderBaseCore : EntityViewProviderBase<VwDoanhSo>
	{
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions
		
		/*
		///<summary>
		/// Fill an VList&lt;VwDoanhSo&gt; From a DataSet
		///</summary>
		/// <param name="dataSet">the DataSet</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList&lt;VwDoanhSo&gt;"/></returns>
		protected static VList&lt;VwDoanhSo&gt; Fill(DataSet dataSet, VList<VwDoanhSo> rows, int start, int pagelen)
		{
			if (dataSet.Tables.Count == 1)
			{
				return Fill(dataSet.Tables[0], rows, start, pagelen);
			}
			else
			{
				return new VList<VwDoanhSo>();
			}	
		}
		
		
		///<summary>
		/// Fill an VList&lt;VwDoanhSo&gt; From a DataTable
		///</summary>
		/// <param name="dataTable">the DataTable that hold the data.</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pagelen">number of row.</param>
		///<returns><see chref="VList<VwDoanhSo>"/></returns>
		protected static VList&lt;VwDoanhSo&gt; Fill(DataTable dataTable, VList<VwDoanhSo> rows, int start, int pagelen)
		{
			int recordnum = 0;
			
			System.Collections.IEnumerator dataRows =  dataTable.Rows.GetEnumerator();
			
			while (dataRows.MoveNext() && (pagelen != 0))
			{
				if(recordnum >= start)
				{
					DataRow row = (DataRow)dataRows.Current;
				
					VwDoanhSo c = new VwDoanhSo();
					c.Id = (Convert.IsDBNull(row["ID"]))?(int)0:(System.Int32)row["ID"];
					c.SoHoaDon = (Convert.IsDBNull(row["So_Hoa_Don"]))?string.Empty:(System.String)row["So_Hoa_Don"];
					c.NgayLayHang = (Convert.IsDBNull(row["Ngay_Lay_Hang"]))?DateTime.MinValue:(System.DateTime?)row["Ngay_Lay_Hang"];
					c.KhachHangId = (Convert.IsDBNull(row["Khach_Hang_ID"]))?Guid.Empty:(System.Guid?)row["Khach_Hang_ID"];
					c.ThanhTien = (Convert.IsDBNull(row["Thanh_Tien"]))?0.0m:(System.Decimal?)row["Thanh_Tien"];
					c.InventoryItemId = (Convert.IsDBNull(row["InventoryItemID"]))?Guid.Empty:(System.Guid?)row["InventoryItemID"];
					c.SoLuong = (Convert.IsDBNull(row["So_Luong"]))?(int)0:(System.Int32?)row["So_Luong"];
					c.DonGia = (Convert.IsDBNull(row["Don_Gia"]))?0.0m:(System.Decimal?)row["Don_Gia"];
					c.GhiChu = (Convert.IsDBNull(row["Ghi_Chu"]))?string.Empty:(System.String)row["Ghi_Chu"];
					c.AcceptChanges();
					rows.Add(c);
					pagelen -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		*/	
						
		///<summary>
		/// Fill an <see cref="VList&lt;VwDoanhSo&gt;"/> From a DataReader.
		///</summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Start row</param>
		/// <param name="pageLength">number of row.</param>
		///<returns>a <see cref="VList&lt;VwDoanhSo&gt;"/></returns>
		protected VList<VwDoanhSo> Fill(IDataReader reader, VList<VwDoanhSo> rows, int start, int pageLength)
		{
			int recordnum = 0;
			while (reader.Read() && (pageLength != 0))
			{
				if(recordnum >= start)
				{
					VwDoanhSo entity = null;
					if (DataRepository.Provider.UseEntityFactory)
					{
						entity = EntityManager.CreateViewEntity<VwDoanhSo>("VwDoanhSo",  DataRepository.Provider.EntityCreationalFactoryType); 
					}
					else
					{
						entity = new VwDoanhSo();
					}
					entity.Id = (System.Int32)reader["ID"];
					//entity.Id = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
					entity.SoHoaDon = reader.IsDBNull(reader.GetOrdinal("So_Hoa_Don")) ? null : (System.String)reader["So_Hoa_Don"];
					//entity.SoHoaDon = (Convert.IsDBNull(reader["So_Hoa_Don"]))?string.Empty:(System.String)reader["So_Hoa_Don"];
					entity.NgayLayHang = reader.IsDBNull(reader.GetOrdinal("Ngay_Lay_Hang")) ? null : (System.DateTime?)reader["Ngay_Lay_Hang"];
					//entity.NgayLayHang = (Convert.IsDBNull(reader["Ngay_Lay_Hang"]))?DateTime.MinValue:(System.DateTime?)reader["Ngay_Lay_Hang"];
					entity.KhachHangId = reader.IsDBNull(reader.GetOrdinal("Khach_Hang_ID")) ? null : (System.Guid?)reader["Khach_Hang_ID"];
					//entity.KhachHangId = (Convert.IsDBNull(reader["Khach_Hang_ID"]))?Guid.Empty:(System.Guid?)reader["Khach_Hang_ID"];
					entity.ThanhTien = reader.IsDBNull(reader.GetOrdinal("Thanh_Tien")) ? null : (System.Decimal?)reader["Thanh_Tien"];
					//entity.ThanhTien = (Convert.IsDBNull(reader["Thanh_Tien"]))?0.0m:(System.Decimal?)reader["Thanh_Tien"];
					entity.InventoryItemId = reader.IsDBNull(reader.GetOrdinal("InventoryItemID")) ? null : (System.Guid?)reader["InventoryItemID"];
					//entity.InventoryItemId = (Convert.IsDBNull(reader["InventoryItemID"]))?Guid.Empty:(System.Guid?)reader["InventoryItemID"];
					entity.SoLuong = reader.IsDBNull(reader.GetOrdinal("So_Luong")) ? null : (System.Int32?)reader["So_Luong"];
					//entity.SoLuong = (Convert.IsDBNull(reader["So_Luong"]))?(int)0:(System.Int32?)reader["So_Luong"];
					entity.DonGia = reader.IsDBNull(reader.GetOrdinal("Don_Gia")) ? null : (System.Decimal?)reader["Don_Gia"];
					//entity.DonGia = (Convert.IsDBNull(reader["Don_Gia"]))?0.0m:(System.Decimal?)reader["Don_Gia"];
					entity.GhiChu = reader.IsDBNull(reader.GetOrdinal("Ghi_Chu")) ? null : (System.String)reader["Ghi_Chu"];
					//entity.GhiChu = (Convert.IsDBNull(reader["Ghi_Chu"]))?string.Empty:(System.String)reader["Ghi_Chu"];
					entity.AcceptChanges();
					rows.Add(entity);
					pageLength -= 1;
				}
				recordnum += 1;
			}
			return rows;
		}
		
		
		/// <summary>
		/// Refreshes the <see cref="VwDoanhSo"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="VwDoanhSo"/> object to refresh.</param>
		protected void RefreshEntity(IDataReader reader, VwDoanhSo entity)
		{
			reader.Read();
			entity.Id = (System.Int32)reader["ID"];
			//entity.Id = (Convert.IsDBNull(reader["ID"]))?(int)0:(System.Int32)reader["ID"];
			entity.SoHoaDon = reader.IsDBNull(reader.GetOrdinal("So_Hoa_Don")) ? null : (System.String)reader["So_Hoa_Don"];
			//entity.SoHoaDon = (Convert.IsDBNull(reader["So_Hoa_Don"]))?string.Empty:(System.String)reader["So_Hoa_Don"];
			entity.NgayLayHang = reader.IsDBNull(reader.GetOrdinal("Ngay_Lay_Hang")) ? null : (System.DateTime?)reader["Ngay_Lay_Hang"];
			//entity.NgayLayHang = (Convert.IsDBNull(reader["Ngay_Lay_Hang"]))?DateTime.MinValue:(System.DateTime?)reader["Ngay_Lay_Hang"];
			entity.KhachHangId = reader.IsDBNull(reader.GetOrdinal("Khach_Hang_ID")) ? null : (System.Guid?)reader["Khach_Hang_ID"];
			//entity.KhachHangId = (Convert.IsDBNull(reader["Khach_Hang_ID"]))?Guid.Empty:(System.Guid?)reader["Khach_Hang_ID"];
			entity.ThanhTien = reader.IsDBNull(reader.GetOrdinal("Thanh_Tien")) ? null : (System.Decimal?)reader["Thanh_Tien"];
			//entity.ThanhTien = (Convert.IsDBNull(reader["Thanh_Tien"]))?0.0m:(System.Decimal?)reader["Thanh_Tien"];
			entity.InventoryItemId = reader.IsDBNull(reader.GetOrdinal("InventoryItemID")) ? null : (System.Guid?)reader["InventoryItemID"];
			//entity.InventoryItemId = (Convert.IsDBNull(reader["InventoryItemID"]))?Guid.Empty:(System.Guid?)reader["InventoryItemID"];
			entity.SoLuong = reader.IsDBNull(reader.GetOrdinal("So_Luong")) ? null : (System.Int32?)reader["So_Luong"];
			//entity.SoLuong = (Convert.IsDBNull(reader["So_Luong"]))?(int)0:(System.Int32?)reader["So_Luong"];
			entity.DonGia = reader.IsDBNull(reader.GetOrdinal("Don_Gia")) ? null : (System.Decimal?)reader["Don_Gia"];
			//entity.DonGia = (Convert.IsDBNull(reader["Don_Gia"]))?0.0m:(System.Decimal?)reader["Don_Gia"];
			entity.GhiChu = reader.IsDBNull(reader.GetOrdinal("Ghi_Chu")) ? null : (System.String)reader["Ghi_Chu"];
			//entity.GhiChu = (Convert.IsDBNull(reader["Ghi_Chu"]))?string.Empty:(System.String)reader["Ghi_Chu"];
			reader.Close();
	
			entity.AcceptChanges();
		}
		
		/*
		/// <summary>
		/// Refreshes the <see cref="VwDoanhSo"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="VwDoanhSo"/> object.</param>
		protected static void RefreshEntity(DataSet dataSet, VwDoanhSo entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.Id = (Convert.IsDBNull(dataRow["ID"]))?(int)0:(System.Int32)dataRow["ID"];
			entity.SoHoaDon = (Convert.IsDBNull(dataRow["So_Hoa_Don"]))?string.Empty:(System.String)dataRow["So_Hoa_Don"];
			entity.NgayLayHang = (Convert.IsDBNull(dataRow["Ngay_Lay_Hang"]))?DateTime.MinValue:(System.DateTime?)dataRow["Ngay_Lay_Hang"];
			entity.KhachHangId = (Convert.IsDBNull(dataRow["Khach_Hang_ID"]))?Guid.Empty:(System.Guid?)dataRow["Khach_Hang_ID"];
			entity.ThanhTien = (Convert.IsDBNull(dataRow["Thanh_Tien"]))?0.0m:(System.Decimal?)dataRow["Thanh_Tien"];
			entity.InventoryItemId = (Convert.IsDBNull(dataRow["InventoryItemID"]))?Guid.Empty:(System.Guid?)dataRow["InventoryItemID"];
			entity.SoLuong = (Convert.IsDBNull(dataRow["So_Luong"]))?(int)0:(System.Int32?)dataRow["So_Luong"];
			entity.DonGia = (Convert.IsDBNull(dataRow["Don_Gia"]))?0.0m:(System.Decimal?)dataRow["Don_Gia"];
			entity.GhiChu = (Convert.IsDBNull(dataRow["Ghi_Chu"]))?string.Empty:(System.String)dataRow["Ghi_Chu"];
			entity.AcceptChanges();
		}
		*/
			
		#endregion Helper Functions
	}//end class

	#region VwDoanhSoFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VwDoanhSo"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VwDoanhSoFilterBuilder : SqlFilterBuilder<VwDoanhSoColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VwDoanhSoFilterBuilder class.
		/// </summary>
		public VwDoanhSoFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VwDoanhSoFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VwDoanhSoFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VwDoanhSoFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VwDoanhSoFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VwDoanhSoFilterBuilder

	#region VwDoanhSoParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="VwDoanhSo"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class VwDoanhSoParameterBuilder : ParameterizedSqlFilterBuilder<VwDoanhSoColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the VwDoanhSoParameterBuilder class.
		/// </summary>
		public VwDoanhSoParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the VwDoanhSoParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public VwDoanhSoParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the VwDoanhSoParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public VwDoanhSoParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion VwDoanhSoParameterBuilder
} // end namespace
